package com.caipiao.crawler.jingqiucai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.caipiao.constant.CrawlerConstants;
import com.caipiao.crawler.model.DaletouResultModel;
import com.caipiao.crawler.model.JingqiucaiResultModel;
import com.caipiao.crawler.model.LotteryFootball;
import com.caipiao.crawler.model.LotteryNumber;
import com.caipiao.crawler.page.BasePageProcessor;
import us.codecraft.webmagic.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by adminitrator on 2018/7/28.
 */
public class JingqiucaiResultPage extends BasePageProcessor {

    @Override
    public void process(Page page) {
        String url = page.getUrl().toString();
        System.out.println(url);
        if(url.contains("history")) {
            //历史页面,获取id
            processHistory(page);
        }else if(url.contains("get_typeBytermList")){
            //比赛数据
            processNowMatchTerm(page);
        }else if(url.contains("match_data")){
            processMatch(page);
        } else {
            //处理接口数据
            processData(page);
        }
    }

    public void processData(Page page) {
        String data = page.getJson().get();
        JSONObject obj = (JSONObject) ((JSONArray) JSON.parse(data)).get(0);
        JSONObject lottery = obj.getJSONObject("lottery");
        LotteryNumber model = new LotteryNumber();
        model.setPrize_detail(obj.getString("details"));
        model.setTerm(lottery.getString("term"));
        model.setNumber(lottery.getString("number"));
        model.setUpdated_time((int) (System.currentTimeMillis()/1000L));
        model.setTotal_sales(lottery.getString("totalSales"));
        model.setPrize_pool(lottery.getString("pool"));
        model.setType(5);
        page.putField("model", model);
    }

    public void processNowMatchTerm(Page page){
        page.setSkip(true);
        String data = page.getJson().get();
        JSONObject obj = (JSONObject) ((JSONArray) JSON.parse(data)).get(0);
        JSONArray tremList = obj.getJSONArray("tremList");
        String term = obj.getString("ctrem"); //当前期数
        String targetUrl = CrawlerConstants.JINGQIUCAI_NOW_URL.replace("qishu",term);
        page.addTargetRequest(targetUrl);
        int index = tremList.indexOf(term);
        if(index>0){
            String nextTerm = (String) tremList.get(index-1);
            targetUrl = CrawlerConstants.JINGQIUCAI_NOW_URL.replace("qishu",nextTerm);
            page.addTargetRequest(targetUrl);
        }
    }

    public void processHistory(Page page) {
        page.setSkip(true);  //不需要传到pipeline
        String id = page.getHtml().xpath("//div[@class='result']/table/tbody/tr/td[1]/text()").toString();
        if(id == null) {
            //TODO error 日志
            return;
        }
        String targetUrl = CrawlerConstants.JINGQIUCAI_DATA_URL + id;
        page.addTargetRequest(targetUrl);
    }

    public void processMatch(Page page) {
//        page.setSkip(true);  //不需要传到pipeline
        String data = page.getJson().get();
        JSONObject obj = (JSONObject) ((JSONArray) JSON.parse(data)).get(0);
        LotteryNumber model = new LotteryNumber();
        JSONObject lterm = obj.getJSONObject("lterm");
        model.setTerm(lterm.getString("term"));
        String openTime_fmt = lterm.getString("openTime_fmt");
        model.setDate(openTime_fmt);
        model.setCreated_time((int) (System.currentTimeMillis()/1000L));
        model.setStart_sale(lterm.getString("sTime"));
        model.setEnd_sale(lterm.getString("fTime"));
        model.setType(5);
        JSONArray array =  obj.getJSONArray("match_vs");
        model.setDetails(array.toString());
        System.out.println("-------------------------");
        System.out.println(model.getDetails());
        page.putField("model", model);
    }
}
